package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.OrderAddress;
import org.springblade.modules.app.service.IOrderAddressService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户表 控制器
 *
 * @author BladeX
 * @since 2020-10-12
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/orderaddress")
@Api(value = "用户表", tags = "用户表接口")
public class OrderAddressController extends BladeController {

    private final IOrderAddressService orderAddressService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入orderAddress")
    public R<OrderAddress> detail(OrderAddress orderAddress) {
        OrderAddress detail = orderAddressService.getOne(Condition.getQueryWrapper(orderAddress));
        return R.data(detail);
    }

    /**
     * 分页 用户表
     */
    @GetMapping("/list")
    public R list(OrderAddress orderAddress) {
        BladeUser bladeUser = AuthUtil.getUser();
        LambdaQueryWrapper<OrderAddress> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.and(wrapper -> wrapper.eq(OrderAddress::getUserid, bladeUser.getUserId()));
//        queryWrapper.last("limit 1,5");
        //查询5条
        queryWrapper.last("limit 5");
        List<OrderAddress> list = orderAddressService.list(queryWrapper);
        return R.data(list);
    }

    /**
     * 自定义分页 用户表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入orderAddress")
    public R<IPage<OrderAddress>> page(OrderAddress orderAddress, Query query) {
        IPage<OrderAddress> pages = orderAddressService.page(Condition.getPage(query), Condition.getQueryWrapper(orderAddress));
        return R.data(pages);
    }

    @PostMapping("/saveAndUse")
    public R saveAndUse(@RequestBody OrderAddress orderAddress) {
        BladeUser bladeUser = AuthUtil.getUser();

        orderAddress.setId(IdWorker.getId());
        orderAddress.setUserid(bladeUser.getUserId() + "");
        orderAddressService.save(orderAddress);
        return R.data(orderAddress);
    }

    /**
     * 新增 用户表
     */
    @PostMapping("/save")
    public R save(@RequestBody OrderAddress orderAddress) {
        return R.status(orderAddressService.save(orderAddress));
    }

    /**
     * 修改 用户表
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入orderAddress")
    public R update(@Valid @RequestBody OrderAddress orderAddress) {
        return R.status(orderAddressService.updateById(orderAddress));
    }

    /**
     * 新增或修改 用户表
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入orderAddress")
    public R submit(@Valid @RequestBody OrderAddress orderAddress) {
        return R.status(orderAddressService.saveOrUpdate(orderAddress));
    }


    /**
     * 删除 用户表
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(orderAddressService.removeByIds(Func.toLongList(ids)));
    }


}
